using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 嘲讽Buff - 强制目标攻击施法者
/// </summary>
public class TauntBuff : BuffBase
{
    private Entity m_OriginalTarget;
    private bool m_HasOriginalTarget;

    public TauntBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 如果目标是僵尸，设置嘲讽目标
        if (Target is Zombie zombie && Caster != null)
        {
            zombie.TauntTarget = Caster;
            m_HasOriginalTarget = true;
        }
        
        // 播放嘲讽特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状态
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("taunted", true);
        }
        
        Logger.Log("TauntBuff", $"{Target.name} 被嘲讽了，强制攻击{Caster.name}");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        // 确保目标持续攻击施法者
        if (Target is Zombie zombie && Caster != null && !Caster.HealthComponent.IsDead)
        {
            zombie.TauntTarget = Caster;
        }
        else if (Target is Zombie zombie2)
        {
            // 如果施法者已死亡，清除嘲讽目标
            zombie2.TauntTarget = null;
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("TauntBuff", $"{Target.name} 嘲讽效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 清除嘲讽目标
        if (Target is Zombie zombie)
        {
            zombie.TauntTarget = null;
        }
        
        // 恢复动画状态
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("taunted", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Taunt"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 嘲讽效果不能叠加，只能刷新持续时间
    }
}